<?php
namespace app\modules\models;

use yii;
use yii\db\ActiveRecord;


 class Admin extends ActiveRecord

 {

    public $rememberMe=true;
    public $repass;
 	public static function tableName(){

 		return "{{%admin}}";
 	}
 	public function attributeLabels(){
        return [
            'adminuser'=>'管理员帐号',
            'adminemail'=>'管理员邮箱',
            'adminpass'=>'管理员密码',
            'repass'=>'确认密码',

        ];
    }
    public function rules(){
    	return [ 
        ['adminuser','required','message'=>'管理员帐号不能为空','on'=>['login','seekpass','changpass','adminadd']],
        ['adminpass','required','message'=>'管理员密码不能为空','on'=>['login','changpass','adminadd']],
        ['rememberMe','boolean','on'=>'login'],
        ['adminpass','validatePass','on'=>'login'],
        ['adminemail','required','message'=>'电子邮箱不能为空','on'=>['seekpass','adminadd']],
        ['adminemail','unique','message'=>'电子邮箱已经被注册','on'=>'adminadd'],
        ['adminuser','unique','message'=>'管理员已经被注册','on'=>'adminadd'],
        ['adminemail','email','message'=>'电子邮箱格式错误','on'=>'seekpass'],
        ['adminemail','validateEmail','on'=>'seekpass'],
        ['repass','required','message'=>'确认密码不能为空','on'=>['changpass','adminadd']],
        ['repass','compare','compareAttribute'=>'adminpass','message'=>'两次密码不一样','on'=>['changpass','adminadd']],
    	 ];
        }
        public function validatePass(){

        	if(!$this->hasErrors()){
        		$data=self::find()->where('adminuser =:user and adminpass=:pass',[":user"=>$this->adminuser,":pass"=>md5($this->adminpass)])->one();
        		if(is_null($data)){
        			$this->addError("adminpass","用户名或者密码错误");
        		}
        	}
        }
        public function validateEmail($data){
            if(!$this->hasErrors()){
                $data=self::find()->where('adminuser=:user and adminemail=:email',[':user'=>$this->adminuser,':email'=>$this->adminemail])->one();
                if(is_null($data)){
                    $this->addError("adminemail","管理员邮箱不匹配");
                }

            }

        }
        public function login($data){
 	          $this->scenario='login';
 	          if($this->load($data)&&$this->validate()){
 	          	$lifetime=$this->rememberMe?24*3600:0;
 	          	$session=YII::$app->session;
 	          	session_set_cookie_params($lifetime);
 	          	$session['admin']=[
 	          	'adminuser'=>$this->adminuser,
 	          	'isLogin'=>1,
 	          	];
 	          	$this->updateAll(['logintime'=>time(),'loginip'=>ip2long(YII::$app->request->userIP)],'adminuser=:user',[':user'=>$this->adminuser]);
 	          	return (bool)$session['admin']['isLogin'];
 	          }else{

 	          	return false;
 	          }
        }
        public function seekPass($data){
        $this->scenario='seekpass';
        if($this->load($data)&&$this->validate()){
            $time=time();
            $token=$this->creatToken($data['Admin']['adminuser'],$time);
            $mailer=Yii::$app->mailer->compose('seekpass',['adminuser'=>$data['Admin']['adminuser'],'time'=>$time,'token'=>$token]);
            $mailer->setFrom('15958615799@163.com');
            $mailer->setTo($data['Admin']['adminemail']);
            $mailer->setSubject('华仔商城-找回密码');
            if($mailer->send()){
                return true;
            }

        } 
        return false;
        }


        public function changPass($data){
         
        $this->scenario='changpass';
       if($this->load($data)&&$this->validate()){
         
        return (bool)$this->updateAll(['adminpass'=>md5($this->adminpass)],'adminuser=:user',[':user'=>$data['Admin']['adminuser']]);

        }
        return false;

        }

        public function reg($data){
            $this->scenario='adminadd';
            
            if($this->load($data) && $this->validate()){



                $this->adminpass=md5($this->adminpass);
                //$this->repass=md5($this->repass);
                if($this->save(false)){
                    return true;
                }else{
                    return false;
                }




            }
        }


        public function creatToken($adminuser,$time){
            return md5(md5($adminuser).base64_encode(Yii::$app->request->userip).md5($time));
        }
    }
 
